package com.promise.jokerdream.ui.toolbar

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.background
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.promise.jokerdream.R

@Composable
fun PMBottomNavigation(
    canGoBack: Boolean = false,
    canGoForward: Boolean = false,
    onBackClick: () -> Unit = {},
    onForwardClick: () -> Unit = {},
    onHomeClick: () -> Unit = {},
    onSettingsClick: () -> Unit = {},
    onExecutionLogClick: () -> Unit = {},
    onGameFunctionClick: () -> Unit = {}
) {
    Surface(
        modifier = Modifier
            .fillMaxWidth()
            .height(60.dp),
        color = Color.White,
        shadowElevation = 2.dp
    ) {
        Row(
            modifier = Modifier
                .fillMaxSize()
                .padding(horizontal = 8.dp),
            horizontalArrangement = Arrangement.SpaceEvenly,
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 后退按钮
            NavigationButton(
                iconRes = R.drawable.ic_left_arrow,
                enabled = canGoBack,
                onClick = onBackClick,
                label = "后退"
            )
            
            // 前进按钮
            NavigationButton(
                iconRes = R.drawable.ic_right_arrow,
                enabled = canGoForward,
                onClick = onForwardClick,
                label = "前进"
            )
            
            // 游戏功能按钮（中间位置）
            NavigationButton(
                iconRes = R.drawable.ic_menu,
                enabled = true,
                onClick = onGameFunctionClick,
                label = "功能"
            )
            
            // 回首页按钮
            NavigationButton(
                iconRes = R.drawable.ic_home,
                enabled = true,
                onClick = onHomeClick,
                label = "首页"
            )
            
            // 执行记录按钮
            NavigationButton(
                iconRes = R.drawable.ic_history,
                enabled = true,
                onClick = onExecutionLogClick,
                label = "记录"
            )
        }
    }
}

@Composable
private fun NavigationButton(
    iconRes: Int,
    enabled: Boolean,
    onClick: () -> Unit,
    label: String,
    isPrimary: Boolean = false
) {
    val contentColor = when {
        isPrimary -> MaterialTheme.colorScheme.primary
        enabled -> MaterialTheme.colorScheme.onSurface
        else -> MaterialTheme.colorScheme.onSurface.copy(alpha = 0.3f) // 更明显的禁用状态
    }
    
    val backgroundColor = when {
        isPrimary -> MaterialTheme.colorScheme.primary.copy(alpha = 0.1f)
        else -> Color.Transparent
    }
    
    IconButton(
        onClick = {
            if (enabled) {
                onClick()
            }
        },
        enabled = enabled,
        modifier = Modifier
            .size(48.dp)
            .then(
                if (isPrimary) {
                    Modifier.background(
                        color = backgroundColor,
                        shape = CircleShape
                    )
                } else {
                    Modifier
                }
            )
    ) {
        Icon(
            painter = painterResource(id = iconRes),
            contentDescription = label,
            tint = contentColor,
            modifier = Modifier.size(if (isPrimary) 26.dp else 24.dp)
        )
    }
}